set mem 500m
set more off
cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\data"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/data/"

use "ti_fdi.dta", clear


cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\output"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/output/"
log using figures.smcl, replace


/****Figure 1****/
/*Transparency International and FDI*/

g fdi_temp=ln_fdi_pc if year==2001
by ccodecow, sort: egen fdi_2001= max(fdi_temp)

#delimit;
twoway 	(lfit ti2 ln_fdi_pc if year==2011, lcolor(black))
		(scatter ti2 ln_fdi_pc if year==2011, msize(vsmall) mcolor(navy) msymbol(square))
		(scatter ti2 ln_fdi_pc if year==2011 & cname=="Vietnam", msize(medsmall) mcolor(red) msymbol(diamond) mlab(cname) mlabcolor(red) mlabsize(small)), 
ytitle(TI: Corruption Perceptions Index, size(medium) margin(medium)) ytitle(, size(small) margin(medsmall)) 
ylabel(, labsize(small)) xtitle(FDI per capita (US Dollars, natural log)) 
xtitle(, size(medium) margin(medium)) xlabel(#6) title(Concurrent Corruption & FDI 2011) 
title(, size(medium)) caption(Correlation Coeff: -0.573*, size(vsmall) position(5) margin(zero)) legend(off);
graph save ti_fdi_2011.gph, replace;


#delimit;
twoway 	(lfit ti2 fdi_2001 if year==2011, lcolor(black))
		(scatter ti2 fdi_2001 if year==2011, msize(vsmall) mcolor(navy) msymbol(square))
		(scatter ti2 fdi_2001 if year==2011 & cname=="Vietnam", msize(medsmall) mcolor(red) msymbol(diamond) mlab(cname) mlabcolor(red) mlabsize(small)), 
ytitle(TI: Corruption Perceptions Index, size(medium) margin(medium)) ytitle(, size(small) margin(medsmall)) 
ylabel(, labsize(small)) xtitle(FDI per capita (US Dollars, natural log)) 
xtitle(, size(medium) margin(medium)) xlabel(#6) title(Corruption in 2011 & FDI 2001) 
title(, size(medium)) caption(Correlation Coeff: -0.647*, size(vsmall) position(5) margin(zero)) legend(off);
graph save  ti_fdi_2001.gph, replace;

#delimit;
graph combine ti_fdi_2011.gph ti_fdi_2001.gph, xcommon ycommon iscale(vsmall);
graph save  Figure1.gph, replace;


/****Figure 2****/
/*HHI GRAPHIC*/

#delimit cr
cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\data"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/data/"

use "HHI_Collapse_2242013_4dig.dta", clear

cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\output"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/output/"


#delimit;
preserve;
collapse (mean) HHI* ln_profit* profit* soe fdi soe_share (semean) se_HHI=HHI se_HHI_inv=HHI_inv se_ln=ln_profit se_profit=profit_margin se_soe1=soe_share se_soe2=soe, by(reg_year restrict);
drop if restrict==.;

generate hi_HHI=HHI+se_HHI*(1.6);
generate low_HHI=HHI-se_HHI*(1.6);
generate hi_HHIinv=HHI_inv+se_HHI_inv*(1.6);
generate low_HHIinv=HHI_inv-se_HHI_inv*(1.6);

#delimit;
generate hi_soe_share=soe_share+se_soe1*(1.6);
generate low_soe_share=soe_share-se_soe1*(1.6);
#delimit;
generate hi_ln_profit=ln_profit+se_ln*(1.6);
#delimit;
generate low_ln_profit=ln_profit-se_ln*(1.6);


#delimit;
twoway (line HHI reg_year if restrict==1, lwidth(medthin) lcolor(maroon)) 
		(line  HHI reg_year if restrict==0, lwidth(medthin) lcolor(navy) lpattern(dash))
, xlab(2000(1)2011, labsize(vsmall)) xtitle("") ylab(.1(.05).5, labsize(small)) ytitle("Herfindahl Index", margin(medsmall))
legend(size(vsmall) rows(2)  label(1 Restricted Sector) label(2 Unrestricted Sector)
position(8) ring(0)) title("Herfindahl Index by Revenue");
graph save HHI.gph, replace;


#delimit;
twoway (line HHI_inv reg_year if restrict==1, lwidth(medthin) lcolor(maroon)) 
		(line  HHI_inv reg_year if restrict==0, lwidth(medthin) lcolor(navy) lpattern(dash))
, xlab(2000(1)2011, labsize(vsmall)) xtitle("") ylab(.1(.05).5, labsize(small)) ytitle("Herfindahl Index", margin(medsmall))
legend(size(vsmall) rows(2)  label(1 Restricted Sector) label(2 Unrestricted Sector)
position(8) ring(0)) title("Herfindahl Index by Revenue");
graph save HHI_inv.gph, replace;


#delimit;
twoway (line soe_share reg_year if restrict==1, lwidth(medthin) lcolor(maroon)) 
		(line  soe_share reg_year if restrict==0, lwidth(medthin) lcolor(navy) lpattern(dash))
, xlab(2000(1)2011, labsize(vsmall)) xtitle("") ylab(.1(.1)1, labsize(small)) ytitle("SOE Share", margin(medsmall))
legend(size(vsmall) rows(2)  label(1 Restricted Sector) label(2 Unrestricted Sector)
position(8) ring(0)) title("SOE Share of Sectoral Investment") legend(off) ;
graph save SOE.gph, replace;

#delimit;
twoway (line ln_profit reg_year if restrict==1, lwidth(medthin) lcolor(maroon)) 
		(line  ln_profit  reg_year if restrict==0, lwidth(medthin) lcolor(navy) lpattern(dash))
, xlab(2000(1)2011, labsize(vsmall)) xtitle("") ylab(, labsize(small)) ytitle("Profit Margin (ln)", margin(medsmall))
legend(size(vsmall) rows(2)  label(1 Restricted Sector) label(2 Unrestricted Sector)
position(8) ring(0)) title("Profit Margin (ln)") legend(off) ;
graph save profit.gph, replace;

#delimit;
graph combine HHI_inv.gph profit.gph,  cols(2) xcommon imargin(vsmall);
graph save Figure2.gph, replace;


lab var reg_year "Year of Census";
restore;
#delimit cr;

/******************************************** Appendix 7b ********************************************/
#delimit;
preserve; 

g ln_labor=ln(ld11+1);
lab var ln_labor "labor at beginning of year (ln)";

collapse (mean) von_nn ts11 ts12 kqkdc ld11 ld21 employment_growth investment_growth profit_margin profit_margin_atax sector_rev 
rev_share sector_count HHI sector_inv inv_share HHI_inv sector_lab lab_share HHI_lab kl_begin 
ln_kl ln_profit ln_labor private soe fie  FDI_inv state_inv private_inv soe_share fdi_share, by(isic4_no reg_year);

cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\do";
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/do/";

do "2014_03_08_MGJ_Money_restricted_FIL_only.do";
replace restrict=0 if restrict==.;

cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\output";
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/output/";


#delimit;
set matsize 1000;
xtset isic4_no reg_year;
replace ld11=ld11/1000;

#delimit;
set more off;
xi: reg HHI restrict ln_labor ln_kl , robust;
outreg2 using appendix_7b, e(rmse) bdec(3) tdec(3) replace;
xi: ivreg2 HHI ln_labor ln_kl (restrict=l.soe) , robust;
outreg2 using appendix_7b, e(all) bdec(3) tdec(3);
xi: reg ln_profit restrict ln_labor ln_kl, robust;
outreg2 using appendix_7b, e(rmse) bdec(3) tdec(3);
xi: ivreg2 ln_profit ln_labor ln_kl (restrict=l.soe), robust;
outreg2 using appendix_7b, e(all) bdec(3) tdec(3);
xi: reg restrict l.soe_share ln_labor ln_kl , robust;
outreg2 using appendix_7b, e(rmse) bdec(3) tdec(3);
predict predict_restrict;

#delimit;
xi: reg HHI predict_restrict ln_labor ln_kl, robust;
xi: reg ln_profit predict_restrict ln_labor ln_kl, robust;
restore;


/****Figure 3****/
#delimit cr;
/*Range Plots*/
cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\data"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/data/"

use 20140308_money_d1.dta, clear

cap cd "C:\Users\ejm5\Dropbox\Rents_Asunder\AJPS_ReplicationFiles\output"
cap cd "/Users/dimitargueorguiev/Dropbox/Rents_Asunder/AJPS_ReplicationFiles/output/"


/*Full Sample*/
mean reg_corrupt if reg_year>2000, over(treatment)
mat list e(b)
mat mean =e(b)'
mat list e(V)
mat var =e(V)'

preserve
collapse reg_corrupt if reg_year>2000, by(treatment)
svmat mean 
svmat var
generate variance=var1+var2
drop var1 var2
drop if treatment==.
scalar define t_crit=invttail(19806, .025)
display t_crit
generate high=mean+(sqrt(var)*t_crit)
generate low=mean-(sqrt(var)*t_crit)
format  mean1 %9.4g

generate year=1
generate year_plus=year+.5

tsset treatment
generate bribe=d.mean
generate bribe2=bribe*100
format bribe2 %9.3g
replace bribe2=round(bribe2, 2)

generate avg_activity=(mean+l.mean)/2
generate label1="% Bribe"
egen label2 = concat(bribe2 label1)

twoway (rcap low high year if treatment==1, lwidth(medthick) lcolor(navy)) ///
(scatter mean year if treatment==1, mlab(mean) mlabcolor(black) mlabposition(3) msymbol(diamond) mcolor(maroon)) ///
(rcap low high year if treatment==0, lwidth(medthick) lcolor(purple) lpattern(dash)) ///
(scatter mean year if treatment==0, mlab(mean) mlabcolor(black) mlabposition(3) msymbol(square) mcolor(blue)) ///
(rspike l.mean mean year_plus, lwidth(medthin) lcolor(black) lpattern(shortdash)) ///
(scatter avg_activity year_plus, msymbol(i) msize(tiny) mcolor(black) mlab(label2) mlabcolor(maroon) mlabsize(small) mlabposition(9)), ///
 xlab(0(1)2) xtitle("") ytitle("Number of Activities during Registration", size(medium) margin(medsmall)) ylab(1(.1)2, labsize(small)) ///
title("Full Sample", size(large)) xscale(off) ///
legend(size(vsmall) position(7) ring(0) label(1 95% CI		Treatment) label(2 Mean) label(3 95% CI		 Control) label(4 Mean) label(5 95% CI for Bribe Frequency) label(6 ""))
graph save f3a.gph, replace
restore


/*Multiple Years*/
mean reg_corrupt if reg_year>2000, over(treatment year)
mat list e(b)
mat mean =e(b)'
mat list e(V)
mat var =e(V)'

preserve
collapse reg_corrupt if reg_year>2000, by(treatment year)
svmat mean 
svmat var
generate variance=var1+var2+var3+var4+var5+var6
drop var1 var2 var3 var4 var5 var6
drop if treatment==.
scalar define t_crit=invttail(19806, .025)
display t_crit
generate high=mean+(sqrt(var)*t_crit)
generate low=mean-(sqrt(var)*t_crit)
format  mean1 %9.4g


generate year_plus=year+.5

xtset year treatment
generate bribe=d.mean
generate bribe2=bribe*100
format bribe2 %9.3g
replace bribe2=round(bribe2, 2)

generate avg_activity=(mean+l.mean)/2
generate label1="% Bribe"
egen label2 = concat(bribe2 label1)

twoway (rcap low high year if treatment==1 & year<2012, lwidth(medthick) lcolor(navy)) ///
(scatter mean year if treatment==1 & year<2012, mlab(mean) mlabcolor(black) mlabposition(3) msymbol(diamond) mcolor(maroon)) ///
(rcap low high year if treatment==1 & year==2012, lwidth(medthick) lcolor(navy)) ///
(scatter mean year  if treatment==1 & year==2012, mlab(mean) mlabcolor(black)  mlabposition(9) msymbol(diamond) mcolor(maroon)) ///
(rcap low high year if treatment==0 & year<2012, lwidth(medthick) lcolor(purple) lpattern(dash)) ///
(scatter mean year if treatment==0 & year<2012, mlab(mean) mlabcolor(black) mlabposition(3) msymbol(square) mcolor(blue)) ///
(rcap low high year if treatment==0 & year==2012, lwidth(medthick) lcolor(purple) lpattern(dash)) ///
(scatter mean year  if treatment==0 & year==2012, mlab(mean) mlabcolor(black)  mlabposition(9) msymbol(square) mcolor(blue)) ///
(rspike l.mean mean year_plus, lwidth(medthin) lcolor(black) lpattern(shortdash)) ///
(scatter avg_activity year_plus, msymbol(i) msize(tiny) mcolor(black) mlab(label2) mlabcolor(maroon) mlabsize(small) mlabposition(9)) ///
, xlab(2010(1)2012) xtitle("") ytitle("Number of Activities during Registration", size(medium) margin(medsmall)) ylab(1(.1)2, labsize(small)) legend(off) ///
title("By Survey Year", size(large)) 
graph save f3b.gph, replace

restore

graph combine f3a.gph f3b.gph, ycommon imargin(vsmall)
graph save figure3.gph, replace

/******************************************** Appendix8 *********************************************/

label define treatment 1 "Treated" 0 "Control"
label values treatment treatment
label define FDI 1 "FDI" 0 "Domestic"
label values FDI "FDI"

histogram reg_corrupt, bcolor(ltblue) barwidth(.5) percent   ///
ytitle(Respondent Share (%)) yscale(line) ylabel(#5) xtitle("", size(zero)) ///
by(, title(Number of Activities During Registration)) by(, legend(off)) by(FDI treatment) addlabel addlabopts(mlabsize(small) mlabposition(0)  mlabcolor(navy)) 
graph save A8_Floor_Ceiling.gph, replace

log close
